﻿using Imaginecup.util.input;
using imaginecup.objectManager;
using imaginecup.information;
using Imaginecup.gameState.malaria.componentInit;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;
using Imaginecup.gameState.malaria.component.util;
using Imaginecup.util;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util.script.malaria;

namespace Imaginecup.gameState.malaria.component.entity.item
{
    public class CmpSliceAttack : BaseCoolTimeItemEntityComponent

    {
        private TouchTrailLayer m_TouchTrail;
        private AttackInfo m_AttackInfo;
        private double m_UseAbleTime;
        private double m_UsingCalculateTime;
        

        public CmpSliceAttack(GObjectID a_GID, ObjectManager objMan)
            : base(a_GID, objMan, ComponentTypeID.CID_ITEM_ENTITY)
        {
            m_TouchTrail = TouchTrailLayer.GetTouchTrailLayer();
            m_AttackInfo = new AttackInfo();
            m_AttackInfo.attackID = AttackID.ATTACK_SLICE;

            m_CoolTime = 10;
            m_UseAbleTime = 3; 
//#if WINDOWS 
//            m_CoolTime = ScriptReader.GetScriptReader().GetDataToFloat( "슬라이드쿨타임");
//            m_UseAbleTime = ScriptReader.GetScriptReader().GetDataToFloat("슬라이드시간");
//#endif

        }

        public override EMessageResult HandleMessage(GMessage a_Msg)
        {            
            return base.HandleMessage(a_Msg);
        }

        public override bool IsUseAble()
        {
            if( m_State ==  CoolState.None )
            {
                m_UsingCalculateTime = m_UseAbleTime;
            }
            return base.IsUseAble();
        }
              
        public override void UseItem(GameTime gameTime)
        {
#if WINDOWS_PHONE
            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();
            TouchCollection collection = input.GetCurrentTouch();

            GeneratorSliceEffect( collection );

            foreach (TouchLocation touch in collection)
            {
                if (m_TouchTrail.IsContain(touch) == false) continue;

                TouchLocation prevLocation;
                touch.TryGetPreviousLocation(out prevLocation);

                if (prevLocation.State == TouchLocationState.Invalid) continue;


                SliceInfo sliceInfo = new SliceInfo();

                sliceInfo.lastPosition = m_TouchTrail.GetTopPath(touch);
                sliceInfo.prevPosition = prevLocation.Position;

                m_AttackInfo.extraData = sliceInfo;


                GMessage message = new GMessage(MessageTypeID.MT_CHECK_ATTACK);
                message.m_Data = m_AttackInfo;

                m_ObjectMan.BroadCastMessage(message, ComponentTypeID.CID_MALARIA_ENTITY);

                m_UsingCalculateTime -= gameTime.ElapsedGameTime.TotalSeconds;

                if (m_UsingCalculateTime < 0)
                {
                    m_UsingCalculateTime = 0;
                    SetClear();
                    m_CoolCalculateTime = m_CoolTime;
                    m_State = CoolState.Delay;
                    m_bEnd = true;
                    m_bSelectItem = false;
                }

            }

#endif
#if WINDOWS || XBOX
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            GeneratorSliceEffect( );


            if( m_TouchTrail.IsGenerator()  )
            {
                
                SliceInfo sliceInfo = new SliceInfo();

                sliceInfo.lastPosition = m_TouchTrail.GetTopPath();
#if XBOX
                sliceInfo.prevPosition = input.PrevPosition;
#else
                
                sliceInfo.prevPosition = new Vector2(input.PreviousMouseState.X , input.PreviousMouseState.Y);
#endif
                m_AttackInfo.extraData = sliceInfo;


                GMessage message = new GMessage(MessageTypeID.MT_CHECK_ATTACK);
                message.m_Data = m_AttackInfo;

                m_ObjectMan.BroadCastMessage(message, ComponentTypeID.CID_MALARIA_ENTITY);

                m_UsingCalculateTime -= gameTime.ElapsedGameTime.TotalSeconds;

                if (m_UsingCalculateTime < 0)
                {
                    m_UsingCalculateTime = 0;
                    SetClear();
                    m_CoolCalculateTime = m_CoolTime;
                    m_State = CoolState.Delay;
                    m_bEnd = true;
                    m_bSelectItem = false;
                }
            }

#endif
        }
        

#if WINDOWS || XBOX
        private  void GeneratorSliceEffect()
        {
             Vector2 GameScreen = MalariaState.GetMalariaState().GetMainScreenSize();
             IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

#if XBOX
           
            if( mouseInput.GamePadState.Buttons.A == ButtonState.Pressed &&
                mouseInput.PreviousPadState.Buttons.A == ButtonState.Released)
            {                
                  //  m_TouchTrail.TouchBegan((int)mouseInput.Position.X , (int)mouseInput.Position.Y);
                  m_TouchTrail.TouchBegan((int)mouseInput.Position.X , (int)mouseInput.Position.Y, 
                            ResourceMan.GetResourceMan().GetTexture("streak" + RandomUtil.rand.Next(1, 5)), 10);
            }


            else if( mouseInput.GamePadState.Buttons.A  == ButtonState.Pressed )
            {
                if (mouseInput.Position.X > GameScreen.X || mouseInput.Position.Y > GameScreen.Y)
                {
                    m_TouchTrail.TouchEnd(); // 진행중이였을때는 종료시킴.               
                }
                else
                {
                    m_TouchTrail.TouchMove((int)mouseInput.Position.X, (int)mouseInput.Position.Y);
                }
            }

            else if (mouseInput.GamePadState.Buttons.A == ButtonState.Released &&
                mouseInput.PreviousPadState.Buttons.A == ButtonState.Pressed)
            {
                m_TouchTrail.TouchEnd();
            }
#else
            if( mouseInput.MouseState.LeftButton == ButtonState.Pressed &&
                mouseInput.PreviousMouseState.LeftButton == ButtonState.Released)
            {
                m_TouchTrail.TouchBegan((int)mouseInput.Position.X, (int)mouseInput.Position.Y,
                        ResourceMan.GetResourceMan().GetTexture("streak" +  5), 15);
            }

            else if( mouseInput.MouseState.LeftButton == ButtonState.Pressed )
            {
                if (mouseInput.Position.X > GameScreen.X || mouseInput.Position.Y > GameScreen.Y)
                {
                    m_TouchTrail.TouchEnd(); // 진행중이였을때는 종료시킴.               
                }
                else
                {
                    m_TouchTrail.TouchMove((int)mouseInput.Position.X, (int)mouseInput.Position.Y);
                }
            }

            else if( mouseInput.MouseState.LeftButton == ButtonState.Released &&
                mouseInput.PreviousMouseState.LeftButton == ButtonState.Pressed)
            {
                m_TouchTrail.TouchEnd();
            }
#endif
        }
#endif
#if WINDOWS_PHONE
        private void GeneratorSliceEffect(TouchCollection collection)
        {

            Vector2 GameScreen = MalariaState.GetMalariaState().GetMainScreenSize();

            foreach (TouchLocation touch in collection)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    if (touch.Position.X > GameScreen.X || touch.Position.Y > GameScreen.Y)
                        continue; // 게임 진행 영역에서만 이펙트 뿌림

                    m_TouchTrail.TouchBegan(touch, ResourceMan.GetResourceMan().GetTexture("streak5"), 10);
                }
                else if (touch.State == TouchLocationState.Moved)
                {
                    if (touch.Position.X > GameScreen.X || touch.Position.Y > GameScreen.Y)
                    {
                        m_TouchTrail.TouchEnd(touch); // 진행중이였을때는 종료시킴.
                        continue; // 게임 진행 영역에서만 이펙트 뿌림
                    }

                    m_TouchTrail.TouchMove(touch);
                }
                else if (touch.State == TouchLocationState.Released)
                {
                    m_TouchTrail.TouchEnd(touch);
                }

            }
        }

#endif


        protected override void SetClear()
        {

#if WINDOWS_PHONE
            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();
            TouchCollection collection = input.GetCurrentTouch();

            //메뉴상태가 바뀌면 현재 진행중인 터치 이벤트를 다 해제시킨다.
            foreach(TouchLocation touch in collection )
            {
                m_TouchTrail.TouchEnd(touch);
            }
#endif

#if WINDOWS || XBOX
            m_TouchTrail.TouchEnd();
#endif
        }
       

    }
}
